/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.workcell.workdata.im.component.event.APNsCloseEvent;
import com.workcell.workdata.im.component.event.APNsOpenEvent;
import com.workcell.workdata.im.entity.Session;
import com.workcell.workdata.im.repository.SessionRepository;
import com.workcell.workdata.im.service.SessionService;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Service
public class SessionServiceImpl implements SessionService {

    @Resource
    private SessionRepository sessionRepository;

    @Resource
    private ApplicationContext applicationContext;


    private final String host ;

    public SessionServiceImpl() throws UnknownHostException {
        host = InetAddress.getLocalHost().getHostAddress();
    }

    @Override
    public void add(Session session) {

        /*
         * 弱网客户端断开重连，或者IOS客户端重连需要删掉存在的记录
         */
        sessionRepository.delete(session.getUid(),session.getDeviceId());

        session.setBindTime(System.currentTimeMillis());
        session.setState(Session.STATE_ACTIVE);
        session.setHost(host);
        sessionRepository.saveAndFlush(session);
    }

    @Override
    public void delete(Long id) {
        sessionRepository.delete(id);
    }

    @Override
    public void updateState(Long id, byte state) {
        sessionRepository.updateState(id,state);
    }

    @Override
    public void updateLocation(Long uid, String nid, double longitude, double latitude, String location) {
        sessionRepository.updateLocation(uid,nid,longitude,latitude,location);
    }

    @Override
    public void openApns(Long uid,String deviceToken) {
        applicationContext.publishEvent(new APNsOpenEvent(uid,deviceToken));
        sessionRepository.openApns(uid,Session.CHANNEL_IOS);
    }

    @Override
    public void closeApns(Long uid) {
        applicationContext.publishEvent(new APNsCloseEvent(uid));
        sessionRepository.closeApns(uid,Session.CHANNEL_IOS);
    }

    @Override
    public long count(Long uid) {
        return sessionRepository.count(uid);
    }

    @Override
    public void delete() {
        sessionRepository.delete(host);
    }
}
